import org.cicd.enums.PipelineType
import org.cicd.modle.AppInfoVars
import org.cicd.utils.utils

/**
 *
 * @param pipelineType 枚举类型
 * @return
 *
 * @see org.cicd.enums.PipelineType
 *
 */
def call(PipelineType pipelineType = PipelineType.STANDARD , Map<String, Object> appMap = new HashMap<>()) {

    appMap.each{println "key: "+it.key +"  value: " + it.value}

    def cfg_text = libraryResource("pipelineCfg.yaml")
    def cfg = readYaml text: cfg_text  //pipeline utility steps 插件
    def paramMap = (null!=appMap)?appMap:[:]
    def appInfoVars = new AppInfoVars()

    def utils = new utils()
    pipeline {

        agent any

        options {
            skipDefaultCheckout()  //删除隐式checkout scm语句
            disableConcurrentBuilds() //禁止并行
            timeout(time: 1, unit: 'HOURS')  //流水线超时设置1h
            timestamps()

        }

        stages {
            stage('参数预处理') {
                steps {
                    script {
                        appInfoVars = getAppInfoVars(appMap)
                        println "appInfoVarsx: ${appInfoVars}"
                    }
                }
            }
            stage("初始化步骤") {
                steps {
                    script{
                        switch (pipelineType) {
                            case PipelineType.STANDARD:
                                standardPipeline(appInfoVars,appMap)
                                break
                            case PipelineType.DEPLOY:
                                deployPipeline(appInfoVars, appMap)
                                break
                            default:
                                break
                        }
                    }

                }
            }
        }
        //清理工作
        post {
            always {
                script {
                    utils.printInfoMessage("清理工作文件")
//                    utils.clearSpace(["jar", "zip"], paramMap.get("ansible_src"))
                }
            }
        }


    }


}

return this